home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / src / plhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  4.3 KB  |  158 lines

  1. /* $Id: plhist.c,v 1.9 1994/06/30 18:22:10 mjl Exp $
  2.  * $Log: plhist.c,v $
  3.  * Revision 1.9  1994/06/30  18:22:10  mjl
  4.  * All core source files: made another pass to eliminate warnings when using
  5.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  6.  * (now included by plplot.h), and other minor changes.  Now each file has
  7.  * global access to the plstream pointer via extern; many accessor functions
  8.  * eliminated as a result.
  9.  *
  10.  * Revision 1.8  1994/03/23  08:15:17  mjl
  11.  * Some cruft elimination.
  12.  *
  13.  * All external API source files: replaced call to plexit() on simple
  14.  * (recoverable) errors with simply printing the error message (via
  15.  * plabort()) and returning.  Should help avoid loss of computer time in some
  16.  * critical circumstances (during a long batch run, for example).
  17. */
  18.  
  19. /*    plhist.c
  20.  
  21.     Histogram plotter.
  22. */
  23.  
  24. #include "plplotP.h"
  25.  
  26. /*----------------------------------------------------------------------*\
  27.  * void plhist()
  28.  *
  29.  * Draws a histogram of n values of a variable in array data[0..n-1] in
  30.  * the range datmin to datmax using nbin bins. If "oldwin" is 1, the
  31.  * histogram is plotted in the current window. If not, the routine calls
  32.  * "plenv" to set up the graphics environment.
  33. \*----------------------------------------------------------------------*/
  34.  
  35. void
  36. c_plhist(PLINT n, PLFLT *data, PLFLT datmin, PLFLT datmax,
  37.      PLINT nbin, PLINT oldwin)
  38. {
  39.     PLINT i, bin;
  40.     PLFLT *x, *y, dx, ymax;
  41.  
  42.     if (plsc->level < 1) {
  43.     plabort("plhist: Please call plinit first");
  44.     return;
  45.     }
  46.     if (plsc->level < 3 && oldwin) {
  47.     plabort("plhist: Please set up window first");
  48.     return;
  49.     }
  50.     if (datmin >= datmax) {
  51.     plabort("plhist: Data range invalid");
  52.     return;
  53.     }
  54.     if ( ! (x = (PLFLT *) malloc((size_t) nbin * sizeof(PLFLT)))) {
  55.     plabort("plhist: Out of memory");
  56.     return;
  57.     }
  58.     if ( ! (y = (PLFLT *) malloc((size_t) nbin * sizeof(PLFLT)))) {
  59.     free((void *) x);
  60.     plabort("plhist: Out of memory");
  61.     return;
  62.     }
  63.  
  64.     dx = (datmax - datmin) / nbin;
  65.     for (i = 0; i < nbin; i++) {
  66.     x[i] = datmin + i * dx;
  67.     y[i] = 0.0;
  68.     }
  69.  
  70.     for (i = 0; i < n; i++) {
  71.     bin = (data[i] - datmin) / dx;
  72.     bin = bin > 0 ? bin : 0;
  73.     bin = bin < nbin ? bin : nbin - 1;
  74.     y[bin]++;
  75.     }
  76.  
  77.     if ( ! oldwin) {
  78.     ymax = 0.0;
  79.     for (i = 0; i < nbin; i++)
  80.         ymax = MAX(ymax, y[i]);
  81.  
  82.     plenv(datmin, datmax, (PLFLT) 0.0, (PLFLT) (1.1 * ymax), 0, 0);
  83.     }
  84.  
  85.     plbin(nbin, x, y, 0);
  86.     free((void *) x);
  87.     free((void *) y);
  88. }
  89.  
  90. /*----------------------------------------------------------------------*\
  91.  * void plbin()
  92.  *
  93.  * Plot a histogram using the arrays x and y to represent data values
  94.  * and frequencies respectively. If center is false, x values denote the
  95.  * lower edge of the bin, and if center is true, they denote the center
  96.  * of the bin.
  97. \*----------------------------------------------------------------------*/
  98.  
  99. void
  100. c_plbin(PLINT nbin, PLFLT *x, PLFLT *y, PLINT center)
  101. {
  102.     PLINT i;
  103.     PLFLT xmin, xmax, vpwxmi, vpwxma, vpwymi, vpwyma;
  104.  
  105.     if (plsc->level < 3) {
  106.     plabort("plbin: Please set up window first");
  107.     return;
  108.     }
  109.  
  110.     /* Check x[i] are in ascending order */
  111.  
  112.     for (i = 0; i < nbin - 1; i++) {
  113.     if (x[i] >= x[i + 1]) {
  114.         plabort("plbin: Elements of x array must be increasing");
  115.         return;
  116.     }
  117.     }
  118.  
  119.     plP_gvpw(&vpwxmi, &vpwxma, &vpwymi, &vpwyma);
  120.     if ( ! center) {
  121.     for (i = 0; i < nbin - 1; i++) {
  122.         pljoin(x[i], vpwymi, x[i], y[i]);
  123.         pljoin(x[i], y[i], x[i + 1], y[i]);
  124.         pljoin(x[i + 1], y[i], x[i + 1], vpwymi);
  125.     }
  126.     if (x[nbin - 1] < vpwxma) {
  127.         pljoin(x[nbin - 1], vpwymi, x[nbin - 1], y[nbin - 1]);
  128.         pljoin(x[nbin - 1], y[nbin - 1], vpwxma, y[nbin - 1]);
  129.         pljoin(vpwxma, y[nbin - 1], vpwxma, vpwymi);
  130.     }
  131.     }
  132.     else {
  133.     if (nbin < 2)
  134.         return;
  135.     xmin = vpwxmi;
  136.     xmax = MAX(0.5 * (x[0] + x[2]), vpwxmi);
  137.     if (xmin < xmax) {
  138.         pljoin(xmin, vpwymi, xmin, y[0]);
  139.         pljoin(xmin, y[0], xmax, y[0]);
  140.         pljoin(xmax, y[0], xmax, vpwymi);
  141.     }
  142.     for (i = 1; i < nbin - 1; i++) {
  143.         xmin = xmax;
  144.         xmax = MIN(0.5 * (x[i] + x[i + 1]), vpwxma);
  145.         pljoin(xmin, vpwymi, xmin, y[i]);
  146.         pljoin(xmin, y[i], xmax, y[i]);
  147.         pljoin(xmax, y[i], xmax, vpwymi);
  148.     }
  149.     xmin = xmax;
  150.     xmax = vpwxma;
  151.     if (xmin < xmax) {
  152.         pljoin(xmin, vpwymi, xmin, y[nbin - 1]);
  153.         pljoin(xmin, y[nbin - 1], xmax, y[nbin - 1]);
  154.         pljoin(xmax, y[nbin - 1], xmax, vpwymi);
  155.     }
  156.     }
  157. }
  158.